﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using Guoc.DAL.Common;
using Guoc.DAL.Interface;
using Guoc.DAL.CastleIOC;
using Guoc.DAL.Domain;
using System.Configuration;
namespace Guoc.Web.Biz
{
    public class MenuBiz : BaseBusiness
    {
        public readonly static MenuBiz Instance = new MenuBiz();
        IMenu menuService;

        public MenuBiz() : this(null) { }

        public MenuBiz(IMenu imenu)
        {
            //为null取右边值，不为null取左边值
            menuService = imenu == null ? IocFactory<IMenu>.GetService() : imenu;
        }

        public List<Menu> QueryMenu(int page, int rows, out int count)
        {
            return menuService.GetList(page, rows, out count);
        }

        public List<Menu> GetAllMenus()
        {
            List<Menu> list = GetCacheMenuList();
            return list;
        }

        public Dictionary<string, object> GetTreegridFormateMenu()
        {

            List<Menu> list = GetCacheMenuList();
           
            List<Object> result = new List<object>();
            foreach (Menu item in list)
            {
                if (item.ParentID.Trim().Equals("root"))
                {
                    result.Add(new
                    {
                        ID = item.ID,
                        Title = item.Title,
                        Url = item.Url,
                        Sequence = item.Sequence,
                        IsShow = item.IsShow,
                        CreateTime = item.CreateTime.ToString()
                    });
                }
                else
                {
                    result.Add(new
                    {
                        ID = item.ID,
                        Title = item.Title,
                        Url = item.Url,
                        Sequence = item.Sequence,
                        IsShow = item.IsShow,
                        CreateTime = item.CreateTime.ToString(),
                        _parentId = item.ParentID
                    });
                }
            }
            Dictionary<string, object> json = new Dictionary<string, object>();
            List<object> foot = new List<object>();
            foot.Add(new { records = list.Count });
            json.Add("total", list.Count);
            json.Add("rows", result);
            json.Add("footer", foot);

            return json;
        }

        private List<Menu> GetCacheMenuList()
        {
            string isMenuCache = ConfigurationManager.AppSettings.Get("MenuCache");
            List<Menu> list = new List<Menu>();
            if (isMenuCache == "1")
            {
                if (HttpContext.Current.Cache["MenuCache"] == null)
                {
                    list = menuService.GetList();
                    HttpContext.Current.Cache["MenuCache"] = list;
                }
                else
                {
                    list = HttpContext.Current.Cache["MenuCache"] as List<Menu>;
                }
            }
            else
            {
                list = menuService.GetList();
            }
            return list;
        }


        /// <summary>
        /// 加载角色菜单
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public List<Dictionary<string, object>> GetRoleMenu(string roleID)
        {
            StringBuilder html = new StringBuilder();
            List<Menu> list = menuService.GetList();

            List<Dictionary<string, object>> json = new List<Dictionary<string, object>>();

            List<Menu> items = new List<Menu>();
            Dictionary<string, object> menus = new Dictionary<string, object>();

            if (list.Count == 0)
            {
                menus.Add("Title", "菜单管理");
                items.Add(new Menu { Title = "暂无数据" });
                menus.Add("Content", items);
                json.Add(menus);
                return json;
            }

            //查找根菜单
            Menu root = list.FirstOrDefault(m => m.ParentID.Trim() == "root");
            items = new List<Menu>();
            menus.Add("Title", root.Title);
            items.Add(root);
            menus.Add("Content", items);
            json.Add(menus);

            //查找一级子菜单
            List<Menu> rootMenus = list.Where(m => m.ParentID == root.ID).OrderBy(m => m.Sequence).ToList();
            foreach (Menu itemRoot in rootMenus)
            {
               //标题
                menus = new Dictionary<string, object>();
                menus.Add("Title", itemRoot.Title); 

                //查找二级子菜单               
                List<Menu> childMenus = list.Where(m => m.ParentID == itemRoot.ID)
                                        .OrderBy(m => m.Sequence).ToList();
                menus.Add("Content", childMenus);
                json.Add(menus);
            }

            return json;

        }

        public bool CreateMenuRoot(Menu menu)
        {
            if (!menuService.IsExists("ParentID", "root"))
            {
                menu.ParentID = "root";
                return menuService.Create(menu);
            }
            else
            {
                return false;
            }
        }

        public bool Create(Menu menu)
        {
            if (!menuService.IsExists("Title", menu.Title))
            {
                return menuService.Create(menu);
            }
            else
            {
                return false;
            }
        }

        public bool Update(Menu menu)
        {
            return menuService.Update(menu);
        }

        public bool Delete(string menuids)
        {
            if (menuService.IsExists("ParentID",menuids))
            {
                return false;  //还保留有子菜单，不能删除 
            }

            menuids = "'" + menuids + "'";
            return menuService.Delete(menuids);
        }

    }
}